<%
if (session.getAttribute("username") == null) {
	%><jsp:forward page="login.jsp" /><%
}
%>
<jsp:useBean id="user" class="bean.PollUser" scope="request" />
<jsp:setProperty name="user" property="*"/>
<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK" import="java.util.*,bean.PollUser, model.PollUserImpl, model.UserlistImpl, connection.DbConnection, java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"> 
<link rel="stylesheet" href="style/stylesheet.css" type="text/css" >
<title>dbPoll/Poll Administration: Open / Close Voting</title>
<script type="text/javascript">

    function check(form){

        // Clear success msgs
        document.getElementById("success_msg").innerHTML = "";
           
        if(document.forms.form1.define_poll.value==""){
            document.getElementById("define_poll_msg").innerHTML = "<div class=\"error\" style=\"display: block; \" >* Please add poll definition. </div>";
            document.forms.form1.define_poll.focus();   
            return false;   
        } else {
        	document.getElementById("define_poll_msg").innerHTML = "";
        }
        return true;
    }

    function set_poll (form) {
        

</script>
</head>

<body onload="user_type();">
<div class="bg">
<div class='content' >
<jsp:include page="nav.jsp" />
	<div id = "title" class= "pagetitle">Administer Poll Voting<input type="button" style="font-size:8pt;color:white;background-color:lightblue;border:none;" value="Online Help"></div>
	<div class="entrytitle">
	Open / Close Voting
	<div class="entry">
		<form action="selectVotingPollServlet" method="post" name="form1">
		<div class="tableborder"><table class="blockTable">
			<tbody>
			<tr><td style="width:20%" >Select Poll For Voting Administration</td><td style="width:5%"><select name="selected_poll" >
			<%
				// Generate Poll Option List
				DbConnection db = new DbConnection();
				Connection conn = db.getConn();
				PreparedStatement ps;
				String sql;
				
				// Check if poll master. If so only display his polls. 
				if (session.getAttribute("accessibility") != null && session.getAttribute("accessibility").equals(2)) {
					sql = "Select Name, Poll.ID from Poll,User_Poll where Poll.ID = User_Poll.PollID AND Username='"+session.getAttribute("username").toString()+"'"; 
					
				} else {
					sql = "Select Name, Poll.ID from Poll";
				}
				//System.out.println("SQL poll select: "+sql);

				ps = conn.prepareStatement(sql);
				ResultSet res = ps.executeQuery();
				
				while (res.next()) {
					out.println("<option value=\""+res.getString(2)+"\" ");
					// Check if currently selected poll
					if (session.getAttribute("selected_poll") != null && session.getAttribute("selected_poll").equals(res.getString(2))) {
						out.println(" selected ");
					}
					out.println(" >"+res.getString(1)+"</option>");
				}
			%>
			</select></td>
			<td><input type="submit" name="submit" class="button" value="Generate Questions" onclick="return set_poll(this);" /></td> </tr>
			</tbody>
		</table>
		</form>
	</div></div>
	</div>
	
	<div class="entrytitle">
	Questions
	<div class="entry">		
		<form action="SubmitVotingAdminServlet" method="post" name="votingForm">
		<div class="tableborder"><table class="blockTable">
		<thead><tr><td>ID</td><td>Question</td><td>Status</td></tr></thead>
		<tbody>
		<%
			// Generate Question Table
			if (session.getAttribute("selected_poll") == null) {
				out.println("<tr><td colspan=\"3\"> Please select poll and click generate questions</td></tr>");
			} else {
				sql = "Select * from Poll_Question,Question where Poll_Question.QuestionID = Question.QuestionID AND Poll_Question.PollID = "+session.getAttribute("selected_poll").toString();
				//System.out.println("SQL generate quesiton: "+sql);
				ps = conn.prepareStatement(sql);
				ResultSet res2 = ps.executeQuery();
				boolean results = false;
				while (res2.next()) {
					results = true;
					out.println("<tr><td style=\"width:5%\">"+res2.getString("QuestionID")+"</td><td>"+res2.getString("Text")+"</td>");
					
					// Check if question open
					boolean status;
					if (res2.getInt("Status") == 1) {
						status = true;
					} else {
						status = false;
					}
					out.println("<td style=\"width:5%\" ><input name=\""+res2.getInt("QuestionID")+"\" type=\"checkbox\" value=\""+res2.getInt("QuestionID")+"\"");
					
					// Make default checked if quesiton open
					if (status) {
						out.println(" checked ");
					}
					out.println(" \\></td>");
					out.println("</tr>");
				}
				if (!results) {
					out.println("<tr><td colspan=\"3\"> No questions for selected poll</td></tr>");
				}
			}
		%>
		<tr><td colspan="3" ><input type="submit" class="button" value="Update" /></td></tr>
		</tbody>
		</table>
		</form>
	</div>
	</div>
	</div>
	
	<div class="entrytitle">
		Device Applet
	<div class="entry">	
	<% if (session.getAttribute("selected_poll") != null) { %>
			 <script src="http://java.com/js/deployJava.js"></script>
 <script>
    var attributes = {width:500, height:300};
    var parameters = {jnlp_href: 'launch.jnlp', pollid: '<%=session.getAttribute("selected_poll").toString()%>'};
    deployJava.runApplet(attributes, parameters, '1.5');
</script>
<%	} else {
	out.println("Please select a poll and click generate.");
	}
    %>
	</div>
	</div>

	<%//Get all users assigned to the selected poll and display 8 records each row%>
	<div class="entrytitle">
		User
	<div class="entry">	
		<table border="5px lightblue">
		<%	
		if (session.getAttribute("selected_poll") != null){
			ArrayList al = new ArrayList();
			al = UserlistImpl.showUser(session.getAttribute("selected_poll").toString());
			int uc = al.size()-1; 
			for (int i = uc ; i/8 >= 0 ; i=i-8){
		%>
		<tr>
		<%
			if( i >= 8)
				{
				for(int j=0;j<8;j++){
				PollUser pb = (PollUser) al.get(i-j);
		%>
				<td id = "<%=pb.getUsername()%>"><%=pb.getUsername()%></td>
		<%
				}
			}
			else if( i < 8)
			{
				for(int j=0;j<=i;j++){
				PollUser pb = (PollUser) al.get(j);
		%>
				<td bgcolor = 
				<% 
				if (UserlistImpl.changeStatus(session.getAttribute("selected_poll").toString(), pb.getUsername())== false){
					out.println("lightblue");
				} 
				else {
					out.println("blue");
				}  				
				%> 
				Width = "100px" ><%=pb.getUsername()%></td>
		<%
				}
			}
			%>
		</tr>
		<%
			}
		}
		else
		{
			out.println("<tr><td colspan=\"3\"> No selected poll</td></tr>");
		}
		 %>		
			
		</table>
	</div>
	</div>
	<style>
#onlinehelp{position:absolute;display:none;z-index:2;border:5px solid lightblue;background-color:#33aecc;color:white}
</style>
<div id="onlinehelp"></div>
<script>
var demo = document.getElementById("title");
var gg = demo.getElementsByTagName("input");
var ei = document.getElementById("onlinehelp");
for(i=0; i<gg.length; i++){
 var ts = gg[i];
 ts.onmousemove = function(event){
  event = event || window.event;
  ei.style.display = "block";
  ei.innerHTML = '1. Select a Poll and click the "Generate Questions" button<br>2. Select a question by clicking the checkbox and press the "Update" button (Notice: Only one question can be open at the same time)<br>3. If you want to open another question, please deselect the previous one and select another one<br>4. Device Applet <br>5. The user part will show all the users assigned to this poll and after they give responses the grid will change color<br>*For more information please click the button  ';
  ei.style.top  = document.body.scrollTop + "px";
  ei.style.left = document.body.scrollLeft + 300 + "px";
 }
 ts.onmouseout = function(){
  ei.innerHTML = "";
  ei.style.display = "none";
 }
 ts.onclick = function(){
  location.href="/DBP/OnlineHelp/Administerpm.jsp";
 }
}
</script>
</body>
</html>